package com.graduate.examapp

import android.util.Log
import com.blankj.utilcode.util.GsonUtils
import com.google.gson.*
import com.graduate.examapp.entity.ExamTask
import com.graduate.examapp.entity.LoginData
import com.graduate.examapp.request.Response
import org.junit.Test

import org.junit.Assert.*
import retrofit2.converter.gson.GsonConverterFactory
import java.lang.reflect.Type
import java.time.LocalDate

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        //assertEquals(4, 2 + 2)
        val data =
                "        {\n" +
                "            \"courseName\": \"操作系统\",\n" +
                "            \"invigilatorName\": \"王广阔\",\n" +
                "            \"assistantName\": \"肖婧\",\n" +
                "            \"examDate\": \"2023-04-04\",\n" +
                "            \"weekDay\": \"第7周 星期二\",\n" +
                "            \"duration\": \"10:00-12:00\",\n" +
                "            \"studentNum\": 80,\n" +
                "            \"place\": \"会3-203\"\n" +
                "        }"

                // 创建 Gson 对象
        //        val gson = GsonBuilder()
        //            .registerTypeAdapter(LocalDate::class.java, object :  JsonDeserializer<LocalDate> {
        //                override fun deserialize(
        //                    json: JsonElement?,
        //                    typeOfT: Type?,
        //                    context: JsonDeserializationContext?
        //                ): LocalDate {
        //
        //                    var localDate = LocalDate.now()
        //                    json?.let {
        //                        val asString = it.asString
        //                        localDate = LocalDate.parse(asString)
        //                    }
        //                    return localDate
        //                }
        //            }).create()
        //val loginData = gson.fromJson(data, LoginData::class.java)
        //println(loginData)
        val str = "{\"courseName\": \"数据结构\",\n" +
                "        \"invigilatorName\": \"王广阔\",\n" +
                "        \"assistantName\": \"肖婧\",\n" +
                "        \"examDate\": \"2023-06-19\",\n" +
                "        \"weekDay\": \"第18周-星期一\",\n" +
                "        \"duration\": \"18:00-20:00\",\n" +
                "        \"studentNum\": 80,\n" +
                "        \"place\": \"会4-215\"\n" +
                "    }"
        val examTask = GsonBuilder().registerTypeAdapter(LocalDate::class.java, object : JsonDeserializer<LocalDate> {
            override fun deserialize(
                json: JsonElement?,
                typeOfT: Type?,
                context: JsonDeserializationContext?
            ): LocalDate {
                //日当前日期
                var localDate = LocalDate.now()
                json?.let {
                    val asString = it.asString
                    localDate = LocalDate.parse(asString)
                }
                return localDate
            }
        }).create().fromJson(str, ExamTask::class.java)

        //val examTask = GsonUtils.fromJson(str, ExamTask::class.java)
        println(examTask.toString())
    }
}